﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MyHotel.MyHotel_MHThanhToan_BUS_Webservice;
using MyHotel.Report;

namespace MyHotel
{
    public partial class frmThanhToan : DevComponents.DotNetBar.Office2007Form
    {
        private MatHang[] _listMatHang;
        private PhieuDichVu[] _listDichVuOld;
        private List<PhieuDichVu> _listDichVuNew;
        private List<DichVu> _listDV;
        private float _tongtien = 0;
        private PhieuThuePhong _phieuthue;
        private Phong _phong;
        private MHThanhToanBUSWSSoapClient ws; 

        // List<ThongTinThanhToan> listThogtinthanhtoan = new List<ThongTinThanhToan>();
        public frmThanhToan(MyHotel_MHPhong_BUS_Webservice.Phong p)
        {
            setPhong(p);
            _listDV = new List<DichVu>();
            _listDichVuNew = new List<PhieuDichVu>();
            ws = new MHThanhToanBUSWSSoapClient();
            InitializeComponent();
        } 

        private void setPhong(MyHotel_MHPhong_BUS_Webservice.Phong p)
        {
            _phong = new Phong();
            _phong.MaPhong = p.MaPhong;
            _phong.TenPhong = p.TenPhong;
            _phong.TinhTrang = p.TinhTrang;
            _phong.TienPhong = p.TienPhong;
            _phong.GhiChu = p.GhiChu;
            _phong.LoaiPhong = p.LoaiPhong;
        }

        private void frmThanhToan_Load(object sender, EventArgs e)
        {
            _listMatHang = ws.getListMatHang();
            comboBoxDichVu.DataSource = _listMatHang;
            comboBoxDichVu.DisplayMember = "TenMH";
            comboBoxDichVu.ValueMember = "MaMH";

            _listDichVuOld = ws.getListDichVuByMaPhong(_phong.MaPhong);
            foreach (PhieuDichVu pdv in _listDichVuOld)
            {
                DichVu dv = new DichVu();
                dv.SoLuong = pdv.SoLuong;
                dv.DonGia = pdv.GiaMatHang;
                dv.ThoiGian = pdv.NgaySuDung.ToString();
                foreach (MatHang mh in _listMatHang)
                {
                    if (pdv.MaMH == mh.MaMH)
                    {
                        dv.Ten = mh.TenMH;
                        break;
                    }
                }
                _tongtien += pdv.SoLuong * pdv.GiaMatHang;
                _listDV.Add(dv);
                object[] row = { 1, dv.Ten, dv.SoLuong, dv.ThoiGian, dv.DonGia };
                dataGridViewX1.Rows.Add(row);
            }

            _phieuthue = ws.getListPhieuThuePhong(_phong.MaPhong);
            lblNgayVao.Text = _phieuthue.NgayThue.ToString();
            lblNgayRa.Text = DateTime.Now.ToString();
            lblTienPhong.Text = String.Format("{0:0,0}", _phieuthue.DonGia) + " VNĐ";
            lblTenPhong.Text = _phong.TenPhong;
            lblMaHoaDon.Text += _phieuthue.MaPhieu;

            _tongtien += _phieuthue.DonGia;
            lblTongTien.Text = String.Format("{0:0,0}", _tongtien) + " VNĐ";
        } 

        private void btnThem_Click(object sender, EventArgs e)
        {
            int index = comboBoxDichVu.SelectedIndex;
            DichVu dv = new DichVu();
            dv.Ten = _listMatHang[index].TenMH;
            dv.ThoiGian = DateTime.Now.ToString();
            dv.DonGia = _listMatHang[index].GiaBanHienHanh;
            dv.SoLuong = integerInputSoLuong.Value;
            _listDV.Add(dv);

            _tongtien += dv.DonGia * dv.SoLuong;
            lblTongTien.Text = String.Format("{0:0,0}", _tongtien) + " VNĐ";

            PhieuDichVu pdv = new PhieuDichVu();
            pdv.MaMH = _listMatHang[index].MaMH;
            pdv.GiaMatHang = _listMatHang[index].GiaBanHienHanh;
            pdv.MaPhieuThue = _phieuthue.MaPhieu;
            pdv.NgaySuDung = DateTime.Now;
            pdv.SoLuong = integerInputSoLuong.Value;
            _listDichVuNew.Add(pdv);

            object[] row = { 1, dv.Ten, dv.SoLuong, dv.ThoiGian, dv.DonGia };
            dataGridViewX1.Rows.Add(row);
        }

        private void comboBoxDichVu_SelectedIndexChanged(object sender, EventArgs e)
        {
            int index = comboBoxDichVu.SelectedIndex;
            integerInputSoLuong.MaxValue = _listMatHang[index].SoLuong;
        }

        private void dataGridViewX1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            TaoSTT();
        }

        private void dataGridViewX1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            int count = _listDichVuOld.Length;
            if (e.ColumnIndex == 5 && e.RowIndex != -1 && e.RowIndex >= count)
            {
                dataGridViewX1.CurrentRow.Selected = true;

                DialogResult result = MessageBox.Show("Bạn có chắc chắn muốn xóa dòng này không?", "Hướng dẫn", MessageBoxButtons.YesNo, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button2);
                if (result == System.Windows.Forms.DialogResult.No)
                    return;

                DichVu dv = _listDV[e.RowIndex];
                _tongtien -= dv.DonGia * dv.SoLuong;
                lblTongTien.Text = String.Format("{0:0,0}", _tongtien) + " VNĐ";

                dataGridViewX1.Rows.RemoveAt(e.RowIndex);
                _listDV.RemoveAt(e.RowIndex);
                _listDichVuNew.RemoveAt(e.RowIndex - count);
                TaoSTT();
            }
        } 

        private void TaoSTT()
        {
            for (int i = 0; i < dataGridViewX1.Rows.Count; i++)
            {
                dataGridViewX1.Rows[i].Cells[0].Value = i + 1;
            }
        }

        private void Luu()
        {
            foreach (PhieuDichVu pdv in _listDichVuNew)
            {
                ws.themPhieuDichVu(pdv);
            }
        }

        private void btnLuu_Click(object sender, EventArgs e)
        {
            Luu();
            MessageBox.Show("Cập nhật thành công!!", "Thông Báo");
            this.Close();
        }

        private void btnTraPhong_Click(object sender, EventArgs e)
        {
            Luu();
            ws.ThanhToanPhong(_phong.MaPhong, _tongtien);
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            String tenphong = _phong.TenPhong;
            String ngayvao = lblNgayVao.Text;
            String ngayra = lblNgayRa.Text;
            String tienphong = ((_phieuthue.NgayTra - _phieuthue.NgayTra).TotalDays + 1) * _phieuthue.DonGia + "";
            String tongtiendichvu;

            int sum = 0;
            foreach (DichVu dv in _listDV)
            {
                sum += dv.DonGia * dv.SoLuong;
            }
            tongtiendichvu = sum.ToString();
            frmReviewHoaDonThanhToan frm = new frmReviewHoaDonThanhToan(tenphong, ngayvao, ngayra, tienphong, tongtiendichvu, _listDV);
            frm.ShowDialog();
        }
    }

}